package com.jerry.template.module.mainpage.find.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.jerry.template.R;
import com.jerry.template.base.BaseFragment;
import com.jerry.template.constant.Urls;
import com.jerry.template.module.mainpage.find.BannerListActivity;
import com.jerry.template.module.mainpage.find.activity.FindListItemActivity;
import com.jerry.template.module.mainpage.find.adapter.FindListAdapter;
import com.jerry.template.module.mainpage.find.entity.FindMessage;
import com.jerry.template.utils.EventMessage;
import com.jerry.template.utils.OkHttpUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by chenhuan on 2016/5/11.
 */
public class FindDefaultFragment extends BaseFragment{

    @BindView(R.id.find_recycleListView)
    RecyclerView findListView;
    @BindView(R.id.find_banner_image)
    ImageView bannerImage;

    private FindMessage.DataBean.CategoriesBean.CategoryListBean categoryListBean;
    private FindMessage findMessage;
    private List<FindMessage.DataBean.CategoriesBean.CategoryListBean> categoryList = new ArrayList<>();
    private List<FindMessage.DataBean.RotateBannerBean.BannersBean> bannersBeanList = new ArrayList<>();

    private FindListAdapter adapter;
    private ImageLoader imageLoader;

    public FindDefaultFragment(){
        imageLoader = ImageLoader.getInstance();
    }

    @Override
    protected int getLayoutId(){
        return R.layout.frag_faxian_default;
    }

    @Override
    protected void initViews(){
        String bannerUrl = "http://p3.pstatp.com/origin/4110010015aef4806b3";
        //Glide.with(this).load(bannerUrl).into(bannerImage);
        imageLoader.displayImage(bannerUrl,bannerImage);

        bannerImage.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Intent  intent = new Intent(getContext(), BannerListActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });

        LinearLayoutManager manager = new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,true);

        findListView.setItemAnimator(new DefaultItemAnimator());
        findListView.setLayoutManager(manager);

        //添加边距
        findListView.addItemDecoration(new SpaceDecoration(5));

    }

    @Override
    protected void loadData(){
        OkHttpUtils.postSubmitForm(Urls.FIND_URL,null);
    }

    @Override
    protected void postEventResult(@NonNull EventMessage message){
      if(Urls.FIND_URL.equals(message.type)){
           Gson gson = new Gson();
            findMessage = gson.fromJson(message.result, FindMessage.class);
            categoryList.addAll(findMessage.getData().getCategories().getCategory_list());
            bannersBeanList.addAll(findMessage.getData().getRotate_banner().getBanners());


          adapter = new FindListAdapter(getContext(),categoryList);
          findListView.setAdapter(adapter);

          adapter.setOnItemClickListener(new FindListAdapter.OnRecyclerViewItemClickListener(){
              @Override
              public void onItemClick(View view, int position){
                  categoryListBean = categoryList.get(position);
                 /* //获取IconBitmap，直接传递bitmap------------------空指针错误
                  Bitmap IconBitmap = ImageUtils.getBitmapFromUrl(categoryListBean.getIcon_url(), 50 * 1000);
                  ByteArrayOutputStream baos = new ByteArrayOutputStream();
                  IconBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
                  byte [] bitmapByte =baos.toByteArray();*/
                  //跳转至列表详情，传递参数
                  Intent intent = new Intent(getContext(), FindListItemActivity.class);
                  intent.putExtra("category_id", categoryListBean.getBig_category_id());
                  intent.putExtra("name", categoryListBean.getName());
                  intent.putExtra("intro", categoryListBean.getIntro());
                  intent.putExtra("totalUpdates", categoryListBean.getTotal_updates());
                  intent.putExtra("subscribeCount", categoryListBean.getSubscribe_count());
                  /*//传递一个装有bitmap的数组
                  intent.putExtra("iconBitmap", bitmapByte);*/
                  intent.putExtra("iconUrl",categoryListBean.getIcon_url());
                  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  startActivity(intent);
              }
          });

       }
    }

    //自定义设置间距
    private  class SpaceDecoration extends RecyclerView.ItemDecoration{

        private int space ;

        public SpaceDecoration(int space){
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state){
            outRect.top = space;
            outRect.left = space;
            if(parent.getChildAdapterPosition(view) == categoryList.size()-1){
                outRect.right = space;
            }
        }
    }
}
